<script>
  // class的构造方法有两个参数items和pageSize(一页的数量大小，默认值10)
  /*
  有如下方法
  
  - prevPage()
  - nextPage()
  - firstPage()
  - lastPage()
  - goToPage(n)
  - getVisibleItems()
  */

  class Pagination {
    // 你的代码
    constructor(items, pageSize) {
      this.items = items
      this.pageSize = pageSize  //每页记录个数
      this.page = 1 // 当前页
    }
    get pageCount() {// 总页数
      return Math.ceil(this.items.length / this.pageSize)
    }

    prevPage() { // 上一页
      const page = this.page - 1
      this.page = page < 1 ? 1 : page
      return this
    }
    nextPage() { // 下一页
      const page = this.page + 1
      this.page = page > this.pageCount ? this.pageCount
        : page
      return this
    }
    firstPage() {
      this.page = 1
      return this
    }
    lastPage() {
      this.page = this.pageCount
      return this
    }
    goToPage(n) {
      this.page = n < 1 ? 1 : n
      this.page = n > this.pageCount ? this.pageCount : n
      return this
    }
    getVisibleItems() {
      return this.items.slice(
        (this.page - 1) * this.pageSize,
        this.page * this.pageSize
      )
    }
  }

  // 创建实例
  const arr = "abcdefghijklmnopqrstuvwxyz".split("");
  const p = new Pagination(arr, 4);
  console.log(p.getVisibleItems()); // ["a", "b", "c", "d"]

  p.nextPage();
  console.log(p.getVisibleItems());// ["e", "f", "g", "h"]

  p.lastPage();
  console.log(p.getVisibleItems()); // ["y", "z"]

  // 以上翻页方法可以链式调用
  p.nextPage().nextPage()
</script>